package com.example.schoolexam2;


import cn.hutool.core.util.IdUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class testPio {
    public static String IMgFolder="D:/questionPic/";
    public static void main(String[] args)  {
        try{
            XSSFWorkbook sheets = new XSSFWorkbook(new FileInputStream("C:/Users/AWei/Desktop/Test.xlsx"));
            XSSFSheet sheet = sheets.getSheetAt(0);
            //获取最后一行的num，循环从第一行开始 此处从0开始
            int lastRowNum = sheet.getLastRowNum();
            for (int i=1;i<lastRowNum;i++) {
                //获取最后单元格num，即总单元格个数 总格式从1开始计算
                int maxRow = sheet.getRow(i).getLastCellNum();
                System.out.println("-----第" + i + "行的数据如下------");
                for (int rol = 0; rol < maxRow; rol++) {
                    if (rol==0){
                        Cell cell = sheet.getRow(i).getCell(rol);
                        cell.setCellType(CellType.STRING);
                        System.out.println(cell.toString());
                        continue;

                    }
                    System.out.println(sheet.getRow(i).getCell(rol) + " ");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("方法执行了");
    }

    @Test
    void sss() throws Exception{

        XSSFWorkbook sheets = new XSSFWorkbook(new FileInputStream("C:/Users/AWei/Desktop/test2.xlsx"));
        XSSFSheet sheet = sheets.getSheetAt(0);
        //获取最后一行的num，循环从第一行开始 此处从0开始
        int lastRowNum = sheet.getLastRowNum();
        //获取所有图片资源
//        findAllPicture(sheet);
        for (int i=1;i<=lastRowNum;i++){
            //获取最后单元格num，即总单元格个数 总格是从1开始计算
            int maxCell = sheet.getRow(i).getLastCellNum();
            for(int rol=0;rol<maxCell;rol++){
                XSSFCell cell = sheet.getRow(i).getCell(1);
                String stringCellValue = cell.getStringCellValue();
                System.out.println(stringCellValue);
            }
            System.out.println(" ");
        }
    }
    public static void findAllPicture(XSSFSheet sheet) throws Exception{
        //读取出来的excle的图片信息和位置信息存储在该集合中
        Map<String, XSSFPictureData> maplist = new HashMap<String, XSSFPictureData>();
        List<XSSFShape> list = sheet.getDrawingPatriarch().getShapes();  //这里可能会出错
        for (XSSFShape xssfShape : list) {
            if (xssfShape instanceof XSSFPicture) {
                XSSFPicture picture = (XSSFPicture) xssfShape;
                XSSFClientAnchor clientAnchor = picture.getClientAnchor();
                XSSFPictureData pictureData = picture.getPictureData();
                //key=行号+列号
                String key = clientAnchor.getRow1() + "-" + clientAnchor.getCol1();
                System.out.println(key);
                System.out.println(pictureData);
                maplist.put(key, pictureData);

            }
        }
        pic(maplist);
    }
    public static void pic(Map<String, XSSFPictureData> maplist) throws Exception{
        //图片保存相关
        Object[] key1 = maplist.keySet().toArray();
        for (Object o : key1) {
            System.out.println(o);
        }
        for (int i = 0; i < maplist.size(); i++) {
            //获取图片流
            XSSFPictureData xssfPictureData = maplist.get(key1[i]);
            //获取图片所在的指定的单元格的数据
//                    String picName = key1[i].toString();
            String picName = IdUtil.simpleUUID();

            byte[] data = xssfPictureData.getData();
            //图片保存路径
            FileOutputStream out = new FileOutputStream(IMgFolder + picName + ".jpg");
            out.write(data);
            out.close();
        }
    }
    @Test
    void tttasda(){
        String arr="1-3-5-7";
        String[] split = arr.split("-");
        for (String s : split) {
            System.out.println(s);
        }
    }


}
